package falstad;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class RobotChoiceActionListener implements ActionListener {

	Container parent;
	Robot bot;
	RobotDriver driver;
	Maze maze;
	
	ManualDriverKeyListener mdkl;
	
	public RobotChoiceActionListener(Container parent, Robot r, RobotDriver d, Maze m) {
		this.parent = parent;
		this.bot = r;
		this.driver = d;
		this.maze = m;
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		String buttonName = arg0.getActionCommand();
		System.out.println("Button Pressed: "+ buttonName);
		try{
			if (driver.getClass() == manualDriver.class)
			{
				parent.removeKeyListener(mdkl);
			}
		}
		catch(NullPointerException e)
		{
			//Ignore this exception
		}
		switch(buttonName)
		{
		case Constants.BUTTONNAME_CURIOUSGAMBLER:
		{
			bot = new CuriousGamblerRobot();
			driver = new CuriousGamblerDriver();
			break;
		}
		case Constants.BUTTONNAME_GAMBLER:
		{
			bot = new GamblerRobot();
			driver = new GamblerRobotDriver();
			break;
		}
		case Constants.BUTTONNAME_MANUAL:
		{
			bot = new StandardRobot(maze);
			driver = new manualDriver(bot);
			ManualDriverKeyListener mdkl = new ManualDriverKeyListener(parent, bot);
			parent.addKeyListener(mdkl);
			break;
		}
		case Constants.BUTTONNAME_WALLFOLLOWER:
		{
			bot = new WallFollowerRobot(maze);
			driver = new WallFollowerDriver(bot);
			break;
		}
		case Constants.BUTTONNAME_WIZARD:
		{
			bot = new WizardRobot(maze);
			driver = new WizardRobotDriver(bot);
			break;
		}
		case Constants.BUTTONNAME_STARTROBOT:
		{
			try {
				driver.drive2Exit();
			} catch (Exception e) {
				e.printStackTrace();
			}
			break;
		}
		}

	}

}
